package com.twu.biblioteca.resources;

import org.junit.After;
import org.junit.Before;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;

/**
 * Created by olga on 8/06/14.
 */
public class STDStreamSetup
{
    protected final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
    protected InputStream stdin = System.in;

    @Before
    public void setUp()
    {
        //We need to setup input stream to simulate user input
        //Also required to set the system output as well, otherwise crashes
        System.setOut(new PrintStream(outContent));
        String data = "    test 1   \n  test 2  \n   test 3   \n";
        System.setIn(new ByteArrayInputStream(data.getBytes()));
    }

    @After
    public void cleanUp()
    {
        //Clean up stdin setup
        System.setOut(null);
        System.setIn(stdin);
    }

    public void resetInputData(String data)
    {
        System.setIn(new ByteArrayInputStream(data.getBytes()));
    }
}
